package com.xpqh.ksg.common.persistence.dao;

import com.xpqh.ksg.common.persistence.model.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xpqh.ksg.modular.warranty.dto.*;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2025-04-16
 */
public interface KsgWarrantyInfoMapper extends BaseMapper<KsgWarrantyInfo> {

    List<KsgWarrantyInfo> getWarrntyList(@Param("phone") String phone);

    @Select("SELECT product_name productName,term from ksg_product_info where product_code = #{seriesId} limit 0,1")
    KsgProductNameListRespDTO getProductBySeriesId(@Param("seriesId") String seriesId);

    List<KsgCarmembraneInfo> getCarMembraneList(@Param("warrantyId")String warrantyId);

    @Select("select b.user_code userCode, b.user_name operatorName, c.office_code autoId,c.address storeAddress,c.office_code storeNo,c.phone storePhone,\n" +
            "c.office_name storeName from ksg_users a left join js_sys_user b on a.phone = b.login_code\n" +
            "LEFT JOIN js_sys_employee emp on emp.emp_code = b.ref_code left join js_sys_office c on emp.office_code = c.office_code where a.user_id = #{userId}")
    KsgStoreInfo getStoreInfo(@Param("userId") Integer userId);

    @Select("SELECT office_code storeId,use_num useNum FROM ksg_stock_info WHERE signal_id = #{signalId} ")
    KsgStockInfo getBySignalId(String signalId);

    @Select("select a.product_name productName, a.product_code productCode, a.type, a.window_type windowType, a.term from ksg_product_info a left join ksg_stock_info b on a.id = b.product_id where signal_id = #{signalId}")
    KsgProductInfo getProductInfoBySignalId(@Param("signalId") String signalId);

    @Update("update ksg_stock_info set use_num = #{useNum},use_type = #{useType} where signal_id = #{signalId}")
    Integer updateStockInfo(@Param("signalId") String signalId,@Param("useNum") long useNum,@Param("useType")Integer useType);

    @Select("select id from ksg_warranty_info where warranty_number = #{warrantyNumber}")
    String getWarrantyId(@Param("warrantyNumber")String warrantyNumber);

    void insertFileEntity(ImageinfoDto imageinfoDto);

    void insertFileUploadEntity(ImageuploadDto imageuploadDto);

    @Insert("insert into ksg_change_remind(warranty_number,end_time,create_time)" +
            "values(#{warrantyNumber},DATE_ADD(#{endTime},INTERVAL 365 day),now())")
    Integer changeInfo(@Param("warrantyNumber")String warrantyNumber, @Param("endTime") Date endTime);

    KsgWarrantyInfo getWarrntyDetails(Integer id);

}

